//
//  LayerShop.cpp
//  lhq
//
//  Created by codin on 14-7-24.
//
//

#include "LayerShop.h"

Scene* LayerShop::createScene() {
    auto scene = Scene::create();
    auto layer = LayerShop::create();
    scene->addChild(layer);
    return scene;
}

bool LayerShop::init(){
    if (!Layer::init()){
        return false;
    }
    
    auto director = Director::getInstance();
    
    Size visibleSize = director->getVisibleSize();
    Vec2 origin = director->getVisibleOrigin();

    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("texture/shop/shopping_tab_diamond.plist", "texture/shop/shopping_tab_diamond.png");
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("texture/shop/shopping_tab_happybean.plist", "texture/shop/shopping_tab_happybean.png");
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("texture/shop/settingProps.plist", "texture/shop/settingProps.png");
    
//    auto layerColor = LayerColor::create(Color4B(100,100,100,150),visibleSize.width,visibleSize.height);
//    this->addChild(layerColor);
    auto listener = EventListenerTouchOneByOne::create();
    // 屏蔽底层触摸事件
    listener->setSwallowTouches(true);
    listener->onTouchBegan = [](Touch* touch, Event* event){
        return false;
    };
    listener->onTouchMoved = [](Touch* touch, Event* event){};
    listener->onTouchEnded = [=](Touch* touch, Event* event){};
    
    auto bgSp = Sprite::create("texture/common_bg.jpg");
    bgSp->setPosition(origin.x + visibleSize.width - bgSp->getContentSize().width/2,origin.y + bgSp->getContentSize().height/2);
    this->addChild(bgSp);
    // 屏蔽底层触摸事件
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,bgSp);
    
    auto layerVm = this->createRmLayer();
    auto layerRm = this->createVmLayer();
    _layerMultiplex = LayerMultiplex::create(layerVm,layerRm,nullptr);
    this->addChild(_layerMultiplex);
    
    auto closeBtn=MenuItemImage::create("texture/common_dlg_close1.png","texture/common_dlg_close1.png",CC_CALLBACK_1(LayerShop::onCloseClick,this));
    closeBtn->setPosition(visibleSize.width-50,visibleSize.height-50);
    
    _switchBtn0=MenuItemImage::create("texture/common_tab_unselected.png","texture/common_tab_selected.png","texture/common_tab_selected.png",CC_CALLBACK_1(LayerShop::onSwitchClick,this));
    _switchBtn0->setPosition(200,visibleSize.height-60);
    _switchBtn0->setTag(0);
    auto swicthLb0 = LabelTTF::create("购买钻石", "Helvetica", 35);
    swicthLb0->setPosition(100, 55);
    _switchBtn0->addChild(swicthLb0);
    _switchBtn0->setEnabled(false);
    
    _switchBtn1=MenuItemImage::create("texture/common_tab_unselected.png","texture/common_tab_selected.png","texture/common_tab_selected.png",CC_CALLBACK_1(LayerShop::onSwitchClick,this));
    _switchBtn1->setPosition(400,visibleSize.height-60);
    _switchBtn1->setTag(1);
    auto swicthLb1 = LabelTTF::create("兑换豆子", "Helvetica", 35);
    swicthLb1->setPosition(100, 55);
    _switchBtn1->addChild(swicthLb1);
    
    auto menu = Menu::create(closeBtn,_switchBtn0,_switchBtn1,nullptr);
    menu->setPosition(0,0);
    this->addChild(menu,30);
    
    return true;
}

void LayerShop::onCloseClick(Ref* sender){
    this->removeFromParentAndCleanup(true);
}

void LayerShop::onSwitchClick(Ref* sender){
    auto tag = ((MenuItemImage*)sender)->getTag();
    _switchBtn0->setEnabled(tag!=0);
    _switchBtn1->setEnabled(tag==0);
    _layerMultiplex->switchTo(tag);
}

Layer* LayerShop::createRmLayer(){
    Layer* layer = Layer::create();
    
    auto director = Director::getInstance();
    
    Size visibleSize = director->getVisibleSize();
    Vec2 origin = director->getVisibleOrigin();
    
    auto bgSp = Sprite::create("texture/shop/recharge_dlg_bg.jpg");
    bgSp->setPosition(visibleSize.width/2-10,visibleSize.height/2-50);
    layer->addChild(bgSp);
    
    auto ladySp = Sprite::create("texture/shop/shop_lady.png");
    ladySp->setPosition(180,visibleSize.height/2-120);
    layer->addChild(ladySp);
    
    // Create the list view
    auto listView = ListView::create();
    listView->setDirection(ScrollView::Direction::HORIZONTAL);
    listView->setTouchEnabled(true);
    listView->setBounceEnabled(true);
    listView->setPosition(Vec2(visibleSize.width/2+100,visibleSize.height/2-20));
    listView->setAnchorPoint(Vec2(0.5,0.5));
    listView->setContentSize(Size(visibleSize.width-200,550));
    layer->addChild(listView);
    
    int zuanCount[] = {5,10,50,100};
    
    for (int i = 0; i < 4; i++) {
        auto layoutItem = Layout::create();
        layoutItem->setTouchEnabled(true);
        layoutItem->setContentSize(Size(200,400));

        auto spDia = Sprite::create(StringUtils::format("texture/shop/recharge_extra_%d.png",i));
        spDia->setPosition(layoutItem->getContentSize().width/2,layoutItem->getContentSize().height/2+120);
        layoutItem->addChild(spDia);

        auto btn = Button::create();
        btn->setName("TextButton");
        btn->setTag(i);
        btn->setTouchEnabled(true);
        btn->loadTextures("texture/shop/buy_btn.png","texture/shop/buy_btn.png","");
        btn->setPosition(Vec2(layoutItem->getContentSize().width/2,80));
        layoutItem->addChild(btn);
        
        auto spCountBg = Sprite::create("texture/shop/recharge_amount_bg.png");
        spCountBg->setPosition(Vec2(layoutItem->getContentSize().width/2,layoutItem->getContentSize().height/2-20));
        layoutItem->addChild(spCountBg);
        
        auto countLb=LabelTTF::create(StringUtils::format("%d钻.",zuanCount[i]),"Helvetica",30);
        countLb->setPosition(80,20);
        spCountBg->addChild(countLb);
        
        listView->pushBackCustomItem(layoutItem);
    }
    
    return layer;
}

Layer* LayerShop::createVmLayer(){
    Layer* layer = Layer::create();
    
    auto director = Director::getInstance();
    
    Size visibleSize = director->getVisibleSize();
    Vec2 origin = director->getVisibleOrigin();
    
    // Create the list view
    auto listView = ListView::create();
    listView->setDirection(ScrollView::Direction::HORIZONTAL);
    listView->setTouchEnabled(true);
    listView->setBounceEnabled(true);
    listView->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2-20));
    listView->setAnchorPoint(Vec2(0.5,0.5));
    listView->setContentSize(Size(visibleSize.width,500));
    layer->addChild(listView);
    
//    int zuanCount[] = {5,10,50,100};
    float rmCount[] = {2.4,7.3,22,38,65,132,405,700,1420};
    int vmCount[] = {20,60,180,300,500,1000,3000,5000,9999};
    
    for (int i = 0; i < 9; i++) {
        auto layoutItem = Layout::create();
        layoutItem->setTouchEnabled(true);
        layoutItem->setContentSize(Size(300,500));
        layoutItem->setBackGroundColorType(Layout::BackGroundColorType::GRADIENT);
        layoutItem->setBackGroundColor(Color3B(64, 64, 64),Color3B(192, 192, 192));
        
        auto cardBgSp = Sprite::createWithSpriteFrameName("item_cardBack_light.png");
        cardBgSp->setPosition(layoutItem->getContentSize().width/2,layoutItem->getContentSize().height/2+50);
        layoutItem->addChild(cardBgSp);
        
        auto beanBgSp = Sprite::createWithSpriteFrameName("shopping_bean_b_box.png");
        beanBgSp->setPosition(layoutItem->getContentSize().width/2,layoutItem->getContentSize().height/2+100);
        layoutItem->addChild(beanBgSp);

        auto vmCountLb=LabelTTF::create(StringUtils::format("%.1f 万豆",rmCount[i]),"Helvetica",40);
        vmCountLb->setPosition(layoutItem->getContentSize().width/2,50);
        cardBgSp->addChild(vmCountLb);
        
        auto rmCountLb=LabelTTF::create(StringUtils::format("%d 钻石",vmCount[i]),"Helvetica",40);
        rmCountLb->setPosition(layoutItem->getContentSize().width/2,150);
        cardBgSp->addChild(rmCountLb);
        
        auto btn = Button::create();
        btn->setName("TextButton");
        btn->setTag(i);
        btn->setTouchEnabled(true);
        btn->loadTextures("texture/shop/buy_btn.png","texture/shop/buy_btn.png","");
        btn->setPosition(Vec2(layoutItem->getContentSize().width/2,20));
        layoutItem->addChild(btn);
        
        listView->pushBackCustomItem(layoutItem);
    }
    
    listView->setGravity(ListView::Gravity::CENTER_VERTICAL);
    listView->setItemsMargin(10);

    return layer;
}



